﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FlightClient.NewService;

namespace FlightClient
{

    public partial class Form1 : Form
    {
        static NewService.IFlightService flightServiceObj = new NewService.FlightServiceClient();
        City fromCity;
        City toCity;
      
        DateTime departure;
        int noOfSeats;
        ListOfConnections listOfConnections;
        Connection selected;
        public Form1()
        {
            
           InitializeComponent();
           IList<City> cities = flightServiceObj.GetCityList();
           IList<City> theSecondList = flightServiceObj.GetCityList();

           cityFromChoice.DataSource = cities;
           cityFromChoice.DisplayMember = "Name";
           cityToChoice.DataSource = theSecondList;
           cityToChoice.DisplayMember = "Name";
           
            
            //foreach(City c in cities)
            //{
              
            //    cityFromChoice.Items.Add(c);
                
            //}
        }
        public override string ToString()
        {
            return base.ToString();
        }
        
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dateTimePicker1 = (DateTimePicker)sender;
            departure = (DateTime)dateTimePicker1.Value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
        MessageBox.Show("Please fill search with your preferences");
        ni.Visible = false;
        
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void timeLabel_Click(object sender, EventArgs e)
        {

        }

        private void cityFromLabel_Click(object sender, EventArgs e)
        {

        }

        private void userSettingsGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
//foreach (DataGridViewRow row in dataGridView1.SelectedRows)
//            {
//                connectionInfoBox.Text = row.Cells[0].Value.ToString();
                
//            }
        }
        private void cityFromChoice_SelectedIndexChanged(object sender, EventArgs e)
        {

        if (cityFromChoice.SelectedText == "")
            {
            cityFromChoice.Text = "Select departure city";
            }

        else
            {
            cityFromChoice.Text = cityFromChoice.SelectedText;
            }
            
           ComboBox cmb = (ComboBox)sender;
           fromCity = (City)cmb.SelectedItem;
           airportFromName.Text = fromCity.Country; 

        }

        private void airportFromName_TextChanged(object sender, EventArgs e)
        {

        }
        
        private void cityToChoice_SelectedIndexChanged(object sender2, EventArgs e)
        {

        if (cityToChoice.SelectedText == "")
            {
            cityToChoice.Text = "Select arrival city";
            }

        else
            {
            cityToChoice.Text = cityToChoice.SelectedText;
            }

            ComboBox cmb1 = (ComboBox)sender2;
            toCity = (City)cmb1.SelectedItem;
            airportToName.Text = toCity.Country;
        }

        private void noOfSeatsBox_TextChanged(object sender, EventArgs e)
        {
            TextBox seatNoBox = (TextBox)sender;
             noOfSeats = int.Parse(seatNoBox.Text);

        }

        private void searchButton_Click(object sender, EventArgs e)
        {

        AddNewCustomProgressBar();
        AddNewStatusBar();
        AddNewLabel();
       // Form1 form1 = new Form1();
       // form1.Tag = new NewService.FlightServiceClient();
       //form1.Show();

            
            if (checkDest() == true)
            {
                if (checkDate() == true)
                {
                    if (checkSeats() == true)
                    {

                        listOfConnections = flightServiceObj.getListOfConnections(fromCity, toCity, noOfSeats, departure);

                        listView1.Items.Clear();
                        int nr = 0;
                        foreach (Connection c in listOfConnections.ConnectionList)
                        {
                            ListViewItem lvl = new ListViewItem(c.DepartureTime.ToString());
                            lvl.SubItems.Add(c.ArrivalTime.ToString());
                            lvl.SubItems.Add(c.TotalPrice.ToString());
                            lvl.SubItems.Add(c.NumberOfFlightChange.ToString());
                            lvl.Tag = c;
                            listView1.Items.Add(lvl);
                            //.SubItems.Add(c.DepartureTime);
                            nr++;

                            // , , , );

                        }
                    }
                    else
                    {
                        MessageBox.Show("Please choose a number of seats.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
                    }
                }
                else
                {
                    MessageBox.Show("Date error.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
                }
            }
            //MessageBox.Show(listOfConnections.ConnectionList[0].DestinationCity.Name, "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            else
            {
                MessageBox.Show("Please select two different cities for arrival and departure.",
        "Important Note",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation,
        MessageBoxDefaultButton.Button1);
            }
                
            
        }

        private System.Windows.Forms.ProgressBar AddNewCustomProgressBar()
            {
            CustomProgressBar customProgressBar = new CustomProgressBar();
            this.Controls.Add(customProgressBar);
            customProgressBar.Location = new System.Drawing.Point(24, 375);
            customProgressBar.Width = 284;
            customProgressBar.Height = 25;
            customProgressBar.Style = ProgressBarStyle.Blocks;
            customProgressBar.Name = "customerProgressBar";

            for (int i = 0; i < 101; i++)
                {
                customProgressBar.Value = i;
                customProgressBar.DisplayStyle = ProgressBarDisplayText.Percentage;



                System.Threading.Thread.Sleep(3);
                if (i > 0 || i < 20)
                    {
                    customProgressBar.Tag = "Initializing";
                    customProgressBar.Show();
                    }
                if (i > 40)
                    {
                    customProgressBar.CustomText = "Connecting to database";
                    }

                if (i > 60)
                    {
                    customProgressBar.CustomText = "Results are retrieved";
                    }

                if (i > 80)
                    {
                    customProgressBar.CustomText = "Search is completed";
                    }

                System.Threading.Thread.EndCriticalRegion();
                }
            return customProgressBar;
            }

         private System.Windows.Forms.StatusBar AddNewStatusBar()
            {
            System.Threading.Thread.BeginCriticalRegion();
            System.Windows.Forms.StatusBar sb = new System.Windows.Forms.StatusBar();
       
            this.Controls.Add(sb);
            sb.Name = "statusBar";
       
            sb.Text = "Connecting to database";
       
            sb.Text = "Results are retrieved";
         
            sb.Text = "Search is completed"; 

          return sb;
            }

         private System.Windows.Forms.Label AddNewLabel()
             {
             System.Windows.Forms.Label lb = new System.Windows.Forms.Label();
             this.Controls.Add(lb);
             lb.Location = new System.Drawing.Point(45, 430);
             lb.Width = 284;
             lb.Height = 25;
             lb.Name = "dynamicLabel";
             lb.ForeColor = Color.Black;
             lb.Text = "FLIGHT RESULTS ARE DISPLAYED IN TABLE";
             return lb;
             }
        

        private void proceedButton_Click(object sender, EventArgs e)
        {
            Connection n = (Connection)listView1.SelectedItems[0].Tag;

            Form2 popup = new Form2(n, int.Parse(noOfSeatsBox.Text));
            DialogResult dialogresult = popup.ShowDialog();
            
           

        }

        private void dateTimePicker1_ValueChanged_1(object sender, EventArgs e)
        {
            departure = dateTimePicker1.Value;
            //connectionInfoBox.Text = departure.ToString();
        }

        private void connectionInfoBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void airportToName_TextChanged(object sender, EventArgs e)
        {

        }

       
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if(listView1.SelectedItems.Count > 0)
            {
                Connection conn = (Connection)listView1.SelectedItems[0].Tag;
                listView3.Items.Clear();

                //MessageBox.Show(cn.DepartureCity.Name, "My Application",
                //  MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

                foreach (Flight f in conn.Flights)
                {
                    ListViewItem lvl = new ListViewItem(f.FromName.ToString());
                    lvl.SubItems.Add(f.ToName.ToString());
                    lvl.SubItems.Add(f.DepartureTime.ToString());
                    lvl.SubItems.Add(f.ArrivalTime.ToString());
                    listView3.Items.Add(lvl);
                   
                }
            }
            
        }
        private Boolean checkDest()
        {
            Boolean ok = true;
            if (fromCity.Name.Equals(toCity.Name) )
            ok = false;
            return ok;
        }
        private Boolean checkDate()
        {
            Boolean ok=true;
            DateTime a = this.dateTimePicker1.Value.Date;
            DateTime b = this.dateTimePicker2.Value.Date;
            int result = DateTime.Compare(a, b);
            if (result > 0)
                ok = false;
            return ok;
        }
        private Boolean checkSeats()
        {
            
            int n;

            bool isNumeric = int.TryParse(noOfSeatsBox.Text, out n);
            return isNumeric;
        }

        
    }
}
